//
//  HFStatusTableViewCell.swift
//  sinaMicroBlog
//
//  Created by mac on 15/11/17.
//  Copyright © 2015年 mac. All rights reserved.
//

import UIKit
import SnapKit

let StatusCellMargin: CGFloat = 12
let StatusCellIconWitdth: CGFloat = 35

class HFStatusTableViewCell: UITableViewCell {
    
    //创建约束更新
    var bottomConstrains:Constraint?
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        selectionStyle = .None
        setUpUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    //创建数据模型
    var status: HFStatus? {
        didSet {
            originalView.status = status
            //给转发微博数据模型赋值
            self.bottomConstrains?.uninstall()
            if let retweeted = status?.retweeted_status {
                retweetedView.retweetedStatus = retweeted
                retweetedView.hidden = false
                bottomView.snp_updateConstraints(closure: { (make) -> Void in
                    self.bottomConstrains = make.top.equalTo(retweetedView.snp_bottom).constraint
                    
                })
            }else {
                retweetedView.hidden = true
                bottomView.snp_updateConstraints(closure: { (make) -> Void in
                    self.bottomConstrains = make.top.equalTo(originalView.snp_bottom).constraint
                })
            }

        }
    }
    
    private func setUpUI() {
        //添加子控件
        contentView.addSubview(originalView)
        contentView.addSubview(bottomView)
        contentView.addSubview(retweetedView)
        //添加约束
        originalView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(contentView.snp_top)
            make.left.equalTo(contentView.snp_left)
            make.right.equalTo(contentView.snp_right)
        }
        
        retweetedView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(originalView.snp_bottom)
            make.left.equalTo(originalView.snp_left)
            make.right.equalTo(originalView.snp_right)
        }
        
        bottomView.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(contentView.snp_left)
            self.bottomConstrains = make.top.equalTo(retweetedView.snp_bottom).constraint
            make.right.equalTo(contentView.snp_right)
            make.height.equalTo(40)
        }
        
        contentView.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(bottomView.snp_bottom)
            make.top.equalTo(self.snp_top)
            make.left.equalTo(self.snp_left)
            make.right.equalTo(self.snp_right)
        }
    }
    
    //懒加载子控件
    private lazy var originalView: HFStatusOriginalView = HFStatusOriginalView()
    private lazy var bottomView: HFStatusCellBottomView = HFStatusCellBottomView()
    private lazy var retweetedView: HFStatusRweetedView = HFStatusRweetedView()

    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

}
